package com.lemon.smartsafetynew.ui.fragment.ledger;

import android.os.Bundle;
import android.view.View;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BaseTopBtnPullToRefreshFragment;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.event.common.DateEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskVo;
import com.lemon.smartsafetynew.net.services.SafeLedgerServices;
import com.lemon.smartsafetynew.ui.activity.ledger.LedgerPatrolStatisticsActivity;
import com.lemon.smartsafetynew.ui.adapter.ledger.LedgerPatrolRecordAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.ledger.LedgerUtils;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Andrew on 2017/9/14.
 */

public class LedgerPatrolRecordFragment extends BaseTopBtnPullToRefreshFragment<SafePatrolTaskVo, List<SafePatrolTaskVo>> {

    private static String f_state = "";
    private static String f_status = "";


    public static LedgerPatrolRecordFragment newInstance() {

        Bundle args = new Bundle();

        LedgerPatrolRecordFragment fragment = new LedgerPatrolRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onPullRefresh() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("f_state", f_state);
        params.put("company_id", AccountUtil.getCompanyId());
        params.put("f_status", f_status);
        params.put("date", dateTv.getText().toString().trim());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafeLedgerServices
                .init()
                .getSafePatrolRecordData(RefreshType.REFRESH, params, f_status);
    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("f_state", f_state);
        params.put("company_id", AccountUtil.getCompanyId());
        params.put("f_status", f_status);
        params.put("date", dateTv.getText().toString().trim());
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafeLedgerServices
                .init()
                .getSafePatrolRecordData(RefreshType.LOAD_MORE, params, f_status);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<SafePatrolTaskVo> getLemonAdapter() {
        return new LedgerPatrolRecordAdapter(mDataList);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_SAFE_LEDGER_PATROL_RECORD + f_status;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initLabels();
    }

    private void initLabels() {

        dateTv.setText(CommonUtil.secondToTimeStr(System.currentTimeMillis(), DateTypeEnum.DAY));
        spinnerText.setText(LedgerUtils.mPatrolRecordsSpinnerArray[0]);
        spinnerLayout.setVisibility(View.VISIBLE);

        spinnerChooseLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(spinnerChooseLayout, new PopupListener(), Arrays.asList(LedgerUtils.mPatrolRecordsSpinnerArray));
            }
        });

    }

    /**
     * 筛选选中项
     */
    private class PopupListener implements RecyclerTClickListener<String> {
        @Override
        public void onItemClick(String data, int position) {

            spinnerText.setText(data);
            f_status = LedgerUtils.getStatus(data);
            f_state = LedgerUtils.getState(data);

            dismissPopWindow();
            doPullRefresh();
        }
    }

    /**
     * 修改日期
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDateEvent(DateEvent event) {

        if (LedgerPatrolStatisticsActivity.class.getName().equals(event.getTag())) {
            dateTv.setText(CommonUtil.avoidNullString(event.getDate()));
            doPullRefresh();
        }
    }

}
